package com.mjfeed.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mjfeed.dao.ProductDao;
import com.mjfeed.dto.api.ProductJSON;
import com.mjfeed.dto.api.ProductListJSON;
import com.mjfeed.entity.Product;
import com.mjfeed.entity.ProductCategory;
import com.mjfeed.service.ProductService;
import com.mjfeed.util.Pagination;
import com.mjfeed.util.ParamBean;
import com.mjfeed.util.QueryCondition;

@Service("productService")
public class ProductServiceImpl extends BaseServiceImpl implements ProductService {

	@Resource(name="productDao")
	private ProductDao productDao;
	
	@Transactional(readOnly = true)
	public Pagination<Product> getPagination(ParamBean paramBean) {
		List<QueryCondition> queryConditions = new ArrayList<QueryCondition>();
		if (StringUtils.isNotBlank(paramBean.getKeyword())) {
			String field = (paramBean.getField() == null) ? "name" : paramBean.getField();
			queryConditions.add(new QueryCondition(field, QueryCondition.LK, paramBean.getKeyword()));
		}
		if (paramBean.getProductCategoryId() != null && paramBean.getProductCategoryId().intValue() > 0) {
			ProductCategory productCategory = getById(ProductCategory.class, paramBean.getProductCategoryId());
			queryConditions.add(new QueryCondition("productCategory", QueryCondition.EQ, productCategory));
		}
		String orderBy = ""; 
		if (paramBean.getPsort() > 0) {
			if (paramBean.getPsort() == 1) {
				orderBy = "order by price asc";
			}
			if (paramBean.getPsort() == 2) {
				orderBy = "order by price desc";
			}
			if (paramBean.getPsort() == 3) {
				orderBy = "order by favoriteMembers.size desc";
			}
		} else {
			orderBy = "order by id desc";
		}
		Pagination<Product> pagination = (Pagination<Product>)getPagination(Product.class, queryConditions, orderBy, paramBean.getPage(), paramBean.getPageSize());
		pagination.setPageHtml(paramBean.getUrl());
		return pagination;
	}

	@Transactional(readOnly = true)
	public boolean snUnique(String previousSn, String currentSn) {
		if (StringUtils.equalsIgnoreCase(previousSn, currentSn)) {
			return true;
		} else {
			if (productDao.snExists(currentSn)) {
				return false;
			} else {
				return true;
			}
		}
	}

	@Transactional(readOnly = true)
	public Product getProductBySn(String sn) {
		if (sn == null || sn.equals("")) {
			return null;
		}
		List<QueryCondition> queryConditions = new ArrayList<QueryCondition>();
		queryConditions.add(new QueryCondition("sn", QueryCondition.EQ, sn));
		List<Product> products = get(Product.class, queryConditions);
		return (products != null && products.size() > 0) ? products.get(0) : null;
	}
	
	@Override
	public ProductListJSON searchToJSON(Pagination<Product> pagination) {
		if (pagination != null) {
			ProductListJSON productList = new ProductListJSON();
			productList.setCurrentPage(pagination.getCurrentPage());
			productList.setPageSize(pagination.getPageSize());
			productList.setRecordCount(pagination.getRecordCount());
			productList.setPageCount(pagination.getPageCount());
			productList.setPrePage(pagination.getPrePage());
			productList.setNextPage(pagination.getNextPage());
			productList.setHasPrePage(pagination.isHasPrePage());
			productList.setHasNextPage(pagination.isHasNextPage());
			List<ProductJSON> recordList = new ArrayList<ProductJSON>();
			for (Product product : pagination.getRecordList()) {
				ProductJSON productJSON  = new ProductJSON();
				productJSON.setProductId(product.getId());
				productJSON.setSn(product.getSn());
				productJSON.setName(product.getName());
				productJSON.setPrice(product.getPrice());
				productJSON.setStock(product.getStock());
				productJSON.setImageUrl("http://192.168.1.101/skin/images/pro1.jpg");
				productJSON.setMemo(product.getMemo());
				recordList.add(productJSON);
			}
			productList.setRecordList(recordList);
			return productList;
		}
		return null;
	}

	@Override
	public ProductJSON detailToJSON(Product product) {
		if (product != null) {
			ProductJSON productJSON = new ProductJSON();
			productJSON.setProductId(product.getId());
			productJSON.setSn(product.getSn());
			productJSON.setName(product.getName());
			productJSON.setPrice(product.getPrice());
			productJSON.setStock(product.getStock());
			productJSON.setImageUrl("http://192.168.1.101/skin/images/pro1.jpg");
			productJSON.setMemo(product.getMemo());
			productJSON.setIntroduction(product.getIntroduction());
			return productJSON;
		}
		return null;
	}

	@Override
	public ProductListJSON allToJSON(List<Product> products) {
		if (products != null) {
			ProductListJSON productList = new ProductListJSON();
			List<ProductJSON> recordList = new ArrayList<ProductJSON>();
			for (Product product : products) {
				ProductJSON productJSON  = new ProductJSON();
				productJSON.setProductId(product.getId());
				productJSON.setSn(product.getSn());
				productJSON.setName(product.getName());
				productJSON.setPrice(product.getPrice());
				productJSON.setStock(product.getStock());
				productJSON.setImageUrl("http://192.168.1.101/skin/images/pro1.jpg");
				productJSON.setMemo(product.getMemo());
				productJSON.setIntroduction(product.getIntroduction());
				recordList.add(productJSON);
			}
			productList.setRecordList(recordList);
			return productList;
		}
		return null;
	}

	@Override
	public ProductListJSON favoritesToJSON(Set<Product> products) {
		if (products != null) {
			ProductListJSON productList = new ProductListJSON();
			List<ProductJSON> recordList = new ArrayList<ProductJSON>();
			for (Product product : products) {
				ProductJSON productJSON  = new ProductJSON();
				productJSON.setProductId(product.getId());
				productJSON.setSn(product.getSn());
				productJSON.setName(product.getName());
				productJSON.setPrice(product.getPrice());
				productJSON.setStock(product.getStock());
				productJSON.setImageUrl("http://192.168.1.101/skin/images/pro1.jpg");
				productJSON.setMemo(product.getMemo());
				productJSON.setIntroduction(product.getIntroduction());
				recordList.add(productJSON);
			}
			productList.setRecordList(recordList);
			return productList;
		}
		return null;
	}
}
